package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController(value = "/user")
public class UserController {

    @Value("${user.name}")
    private String username;

    @Value("${user.password}")
    private String password;

    @PostMapping("/login")
    public Result login(String username, String password, HttpSession session) {
        Result result = new Result();
        if (this.password.equals(password)) {
            result.setFlag(true);
            session.setAttribute("user", username);
        } else  {
            result.setFlag(false);
            result.setMessage("登录失败");
        }
        return result;
    }

    @GetMapping("/getUsername")
    public String getUsername(HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", session.getAttribute("user"));
        return jsonObject.toJSONString();
    }
}
